﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFrontEnd
{
    public partial class Event : PageBase
    {
        #region Private Members
        private BusinessEntities.Model.Event _event;
        #endregion

        #region Page Handlers
        protected void Page_Load(object sender, EventArgs e)
        {            
            LoadAndValidatePagesEvent();
            if (!IsPostBack)
            {
                InitializeElements();    
            }
            if (_event.DateOfEvent < DateTime.Now)
            {
                countdownRow.Style["display"] = "none";
            }
            else
            {
                startCount.Attributes["data-date"] = _event.DateOfEvent.ToString("MMMM d, yyyy HH:mm:ss");
            }
        }
        #endregion

        #region Private Methods
        private void TurnOnError(string errorString)
        {
            AlertContainer.Style["display"] = "block";
            AlertContainer.Attributes["class"] = "alert alert-error";
            AlertText.InnerHtml = errorString;
        }

        private void TurnOnSuccess(string successString)
        {
            AlertContainer.Style["display"] = "block";
            AlertContainer.Attributes["class"] = "alert alert-success";
            AlertText.InnerHtml = successString;
        }

        private void InitializeElements()
        {
            Title = _event.Name;
            header.InnerHtml = _event.Name + header.InnerHtml;
            eventType.InnerText = typeof(BusinessEntities.Model.EventType).GetDescriptionForEnum(_event.EventType);
            eventTypeSpan.InnerText = _event.Name;
            eventName.Text = _event.Name.Trim();
            MaleActorInput.BackColor = System.Drawing.Color.LightBlue;
            FemaleActorInput.BackColor = System.Drawing.Color.LightPink;
            MaleActorInput.Text = _event.MaleActor;
            FemaleActorInput.Text = _event.FemaleActor;
            DateOfEventInput.Text = _event.DateOfEvent.ToString("dd/MM/yyyy");
            TimeOfEventInput.Text = _event.DateOfEvent.ToString("HH:mm");
            InitializeHalls();
            switch (_event.EventType)
            {
                case BusinessEntities.Model.EventType.Wedding:
                    FemaleActorInput.Visible = MaleActorLabel.Visible
                        = MaleActorInput.Visible
                        = FemaleActorLabel.Visible
                        = true;

                    FemaleActorLabel.InnerText = "The bride's name";
                    MaleActorLabel.InnerText = "The groom's name";
                    break;
                case BusinessEntities.Model.EventType.BarMitzva:
                    MaleActorInput.Visible
                        = MaleActorInput.Visible
                        = true;
                    FemaleActorLabel.Visible = FemaleActorInput.Visible = false;
                    MaleActorLabel.InnerText = "The strapping young lad's name";
                    break;
                case BusinessEntities.Model.EventType.BatMitzva:
                    FemaleActorLabel.Visible
                        = FemaleActorInput.Visible
                        = true;
                    MaleActorLabel.Visible = MaleActorInput.Visible = false;
                    FemaleActorLabel.InnerText = "The young lady's name";
                    break;
                case BusinessEntities.Model.EventType.Brit:
                    FemaleActorInput.Visible = MaleActorLabel.Visible
                        = MaleActorInput.Visible
                        = FemaleActorLabel.Visible
                        = false;
                    break;
                case BusinessEntities.Model.EventType.Engagement:
                    FemaleActorInput.Visible = MaleActorLabel.Visible
                       = MaleActorInput.Visible
                       = FemaleActorLabel.Visible
                       = true;
                    FemaleActorLabel.InnerText = "The bride's name";
                    MaleActorLabel.InnerText = "The groom's name";
                    break;
                default:
                    break;
            }

            var report = BL.GetEventReport(_event);
            //totalGuestsInvited.InnerText = report.TotalGuestsInvited.ToString();
            totalGuestsInvited.Text = report.TotalGuestsInvited.ToString();
            totalGuestsComing.Text = report.TotalGuestsComing.ToString();
            totalGuestsRSVPd.Text = report.TotalGuestsRSVPd.ToString();
            qrlink.Attributes["href"] = "QRCodes.aspx?eid=" + _event.ID.ToString();
        }

        private void InitializeHalls()
        {
            foreach (BusinessEntities.Model.Hall hall in BL.GetAllHalls())
            {
                ListItem li;
                if (hall.City != null)
                {
                    li = new ListItem(hall.Name + " - " + hall.City.Name, hall.ID.ToString());
                }
                else
                {
                    li = new ListItem(hall.Name, hall.ID.ToString());
                }
                if (hall.ID == _event.HallID)
                {
                    li.Selected = true;
                }
                HallInput.Items.Add(li);
            }
        }

        private void LoadAndValidatePagesEvent()
        {
            BusinessEntities.Model.User user = GetCurrentUser();
            BusinessEntities.Model.Event e;
            int eventID;
            if (int.TryParse(Request.QueryString["eventID"],out eventID))
            {
                e = BL.GetEventById(eventID);
                if (e.UserID == user.ID)
                {
                    _event = e;
                }
                else
                {
                    Response.ClearContent();
                    Response.StatusCode = 401;
                    Response.End();
                }
            }
            else
            {
                Response.ClearContent();
                Response.StatusCode = 401;
                Response.End();

            }
        }
        #endregion

        #region Event Handlers
        protected void Delete_Click(object sender, EventArgs e)
        {
            BL.DeleteEvent(_event);
            Response.Redirect("Default.aspx");
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            //Hall
            BusinessEntities.Model.Hall hall = BL.GetHallById(int.Parse(HallInput.SelectedItem.Value));
            _event.Hall = hall;
            string format = "dd/MM/yyyy HH:mm";
            DateTime parseAttempt;
            if (!DateTime.TryParseExact(DateOfEventInput.Text + " " + TimeOfEventInput.Text, format, null, System.Globalization.DateTimeStyles.None, out parseAttempt))
            {
                TurnOnError("The date was not in the correct format.");
                return;
            }
            _event.DateOfEvent = parseAttempt;
            switch (_event.EventType)
            {
                case BusinessEntities.Model.EventType.Wedding:
                case BusinessEntities.Model.EventType.Engagement:
                    _event.FemaleActor = FemaleActorInput.Text;
                    _event.MaleActor = MaleActorInput.Text;
                    break;
                case BusinessEntities.Model.EventType.BarMitzva:
                    _event.MaleActor = MaleActorInput.Text;
                    break;
                case BusinessEntities.Model.EventType.BatMitzva:
                    _event.FemaleActor = FemaleActorInput.Text;
                    break;
                default:
                    break;
            }
            if (BL.UpdateEvent(_event) != null)
            {
                TurnOnSuccess("The event was updated successfully!");
                return;
            }
        }
        #endregion

    }
}